{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1234 is iterable False\n",
      "1234 is iterable True\n",
      "[1, 2, 3, 4] is iterable True\n",
      "{1, 2, 3, 4} is iterable True\n",
      "{'Tom': 22, 'Marry': 23, 'Barry': 25} is iterable True\n",
      "(1, 2, 3, 4) is iterable True\n"
     ]
    }
   ],
   "source": [
    "# 判断列表是否为可迭代对象\n",
    "def is_iterable(param):\n",
    "    try:\n",
    "        iter(param)\n",
    "        return True\n",
    "    except TypeError:\n",
    "        return False\n",
    "params = [1234,'1234',[1,2,3,4],set([1,2,3,4]),{'Tom':22,'Marry':23,'Barry':25},(1,2,3,4)]\n",
    "for param in params:\n",
    "    print('{} is iterable {}'.format(param, is_iterable(param)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import psutil\n",
    "# 显示当前python程序占用内存的大小\n",
    "def show_memory_info(hint):\n",
    "    pid =   os.getpid()\n",
    "    p =   psutil.Process(pid)\n",
    "    info =  p.memory_full_info()\n",
    "    memory = info.uss /1024. /1024\n",
    "    print('{} memory use: {}MB'.format(hint,memory))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "initing iterator memory use: 64.97265625MB\n",
      "after iterator initiated memory use: 3899.0MB\n",
      "4999999950000000\n",
      "after sum called memory use: 3899.0MB\n",
      "CPU times: total: 9.69 s\n",
      "Wall time: 9.98 s\n",
      "------------\n",
      "initing generator memory use: 67.03515625MB\n",
      "after generator initiated memory use: 67.03515625MB\n",
      "4999999950000000\n",
      "after sum called memory use: 66.03515625MB\n",
      "CPU times: total: 8.67 s\n",
      "Wall time: 8.83 s\n"
     ]
    }
   ],
   "source": [
    "def test_iterator():\n",
    "    show_memory_info('initing iterator')\n",
    "    list_1 = [i for i in range(100000000)] # 生成迭代器\n",
    "    show_memory_info('after iterator initiated')\n",
    "    print(sum(list_1))\n",
    "    show_memory_info('after sum called')\n",
    "\n",
    "def test_generator():\n",
    "    show_memory_info('initing generator')\n",
    "    list_1 = (i for i in range(100000000)) # 生成生成器\n",
    "    show_memory_info('after generator initiated')\n",
    "    print(sum(list_1))\n",
    "    show_memory_info('after sum called')\n",
    "%time test_iterator()\n",
    "print('------------')\n",
    "%time test_generator()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object generator at 0x000001AFA4973B80>\n",
      "<generator object generator at 0x000001AFA4973F40>\n",
      "next_1 = 1, next_3 = 1\n",
      "next_1 = 2, next_3 = 8\n",
      "next_1 = 3, next_3 = 27\n",
      "next_1 = 4, next_3 = 64\n",
      "next_1 = 5, next_3 = 125\n",
      "next_1 = 6, next_3 = 216\n",
      "next_1 = 7, next_3 = 343\n",
      "next_1 = 8, next_3 = 512\n",
      "1296 1679616\n"
     ]
    }
   ],
   "source": [
    "def generator(k):\n",
    "    i = 1\n",
    "    while True:\n",
    "        yield i**k\n",
    "        i +=1\n",
    "gen_1 = generator(1)\n",
    "gen_3 = generator(3)\n",
    "print(gen_1)\n",
    "print(gen_3)\n",
    "\n",
    "def get_sum(n):\n",
    "    sum_1,sum_3 = 0,0\n",
    "    for i in range(n):\n",
    "        next_1 = next(gen_1)\n",
    "        next_3 = next(gen_3)\n",
    "        print('next_1 = {}, next_3 = {}'.format(next_1, next_3))\n",
    "        sum_1 += next_1\n",
    "        sum_3 += next_3\n",
    "    print(sum_1*sum_1,sum_3*sum_3)\n",
    "get_sum(8)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 5, 9]\n"
     ]
    }
   ],
   "source": [
    "def index_normal(L,target):\n",
    "    indexs = []\n",
    "    for i,num in enumerate(L):\n",
    "        if num == target:\n",
    "            indexs.append(i)\n",
    "    return indexs\n",
    "print(index_normal([1, 6, 2, 4, 5, 2, 8, 6, 3, 2], 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 5, 9]\n"
     ]
    }
   ],
   "source": [
    "def index_generate(L,target):\n",
    "    for i,num in enumerate(L):\n",
    "        if num == target:\n",
    "            yield i\n",
    "print(list(index_generate([1, 6, 2, 4, 5, 2, 8, 6, 3, 2], 2)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<list_iterator object at 0x00000279CF94A7A0>\n",
      "True\n",
      "<list_iterator object at 0x00000279CF948E80>\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "def is_subsequence(a,b):\n",
    "    b = iter(b)\n",
    "    print(b)\n",
    "    return all( i in b for i in a)\n",
    "print(is_subsequence([1,3,5],[1,2,3,4,5]))\n",
    "print(is_subsequence([1,4,3],[1,2,3,4,5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers (803485453.py, line 11)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[19], line 11\u001b[1;36m\u001b[0m\n\u001b[1;33m    03        print(i)\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers\n"
     ]
    }
   ],
   "source": [
    "def is_subsequence(a,b):\n",
    "    b = iter(b)\n",
    "    print(b)\n",
    "    gen = (i for i in a)\n",
    "    print(gen)\n",
    "    for i in gen:\n",
    "        print(i)\n",
    "    gen = ((i in b) for i in a)\n",
    "    print(gen)\n",
    "    for i in gen:\n",
    "        print(i)\n",
    "    return all(((i in b) for i in a))\n",
    "rs = is_subsequence([1,3,5],[1,3,4,5,6,8])\n",
    "print('--------------------------')\n",
    "print(rs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<list_iterator object at 0x00000279CF71A530>\n"
     ]
    }
   ],
   "source": [
    "b = [1,2,3,4,5]\n",
    "print(iter(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object <genexpr> at 0x00000279CF565840>\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "b = (i for i in range(5))\n",
    "print(b)\n",
    "print(2 in b)\n",
    "print(3 in b)\n",
    "print(5 in b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
